package wads.drawing.node;

import wads.app.*;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Rectangle2D;
import java.util.Properties;
import java.util.Vector;
import javax.swing.JOptionPane;

public class GroupNode extends CaseNode {
//  private static final long serialVersionUID = 1L;

  private static final BasicStroke GROUP_STROKE = new BasicStroke(1.0f);
  private static final int CORNER_RECT_WIDTH = 10;
  private static final int CORNER_RECT_HEIGHT = 10;
  
  private int flexibleDialogHeight = 0;
  
  private Vector<UserNode> nodesInside = new Vector<UserNode>();
  
  private transient BaseWADDialog wNodeDialog;
  protected Properties properties;
  
  public GroupNode(Point anchor) {
    super(anchor, "WAD Group");

    id = ++idCount;
    properties = new Properties();
    
//    super.initContent();
    setAnchorPoint(anchor);
  }
  
  @Override
  public String toString() {
    String members = "(";
    for(UserNode mbs: nodesInside) {
      members += "*"+mbs.getName()+" ";
    }
    members += ")";
    
    return "Members = "+members;
  }
  
//  @Override
//  public CaseNodeDialog createDialog(){
//    return null;
//  }
  
  @Override
  public void draw(Graphics2D g2d){
    g2d.setColor(Color.BLUE);
    g2d.setStroke(GROUP_STROKE);
    
    setNodeShapeBounds2D(getStartX(), getStartY(), getWidth(), getHeight());
    removeAllComponents();
    
    Rectangle2D r2d = new Rectangle2D.Float(getStartX(), getStartY(), getWidth(), getHeight());
    append(r2d);
    
    g2d.drawRect(getStartX(), getStartY(), CORNER_RECT_WIDTH, CORNER_RECT_HEIGHT);
    g2d.drawRect(getStartX(), getStartY(), getWidth(), getHeight());
    
    super.draw(g2d);
  }
  
  @Override
  public void showDialog(CaseEditor pf){
    wNodeDialog = new GroupNodeDialog(pf, this);
//    wNodeDialog.setProperties(properties);
    
    if((wNodeDialog != null) && (wNodeDialog instanceof BaseWADDialog)){
//      wNodeDialog.setProperties(properties);
      wNodeDialog.initDisplay();
      //We set DialogSize here because this Dialog must have Flexible property.
      flexibleDialogHeight = 20+(25*countNodesInside())+200;
      wNodeDialog.setSize(350, flexibleDialogHeight);
      wNodeDialog.setOptionButtonsPanelLocation(flexibleDialogHeight);
      wNodeDialog.setVisible(true);
    }
  }
  
  @Override
  public void closeDialog(int returnVal){
    if(returnVal == JOptionPane.OK_OPTION){
//      properties = wNodeDialog.getProperties();
    }
  }
  
  public Vector<UserNode> getNodes(){
    return nodesInside;
  }
  
  public void setNodes(Vector<UserNode> nodes){
    nodesInside = nodes;
  }
  
  public void addNode(UserNode node){
    nodesInside.addElement(node);
  }
  
  public int countNodesInside(){
    return nodesInside.size();
  }
  
  public boolean isOutsideGroupOwnerBounds(Point p){
    if(p.x>getStartX() && p.x<getStartX()+getWidth() && p.y>getStartY() && p.y<getStartY()+getHeight())
      return false;
    else
      return true;
  }
  
  public void refreshNodesMember(){
    for(int i=0;i<nodesInside.size();i++){
      nodesInside.elementAt(i).setStartX(nodesInside.elementAt(i).getGroupStartXDistance()+this.getStartX());
      nodesInside.elementAt(i).setStartY(nodesInside.elementAt(i).getGroupStartYDistance()+this.getStartY());
    }
  }
  
  public boolean removeNodeMember(UserNode wn){
    return nodesInside.remove(wn);
  }
}
